/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.CacheEntryBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.CacheExtension;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.CacheEntry;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.PermissionCache;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.gal.GalGroup;
/*     */ import com.zimbra.cs.httpclient.URLUtil;
/*     */ import com.zimbra.cs.util.SkinUtil;
/*     */ import com.zimbra.cs.util.WebClientL10nUtil;
/*     */ import com.zimbra.cs.util.WebClientServiceUtil;
/*     */ import com.zimbra.cs.zimlet.ZimletUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import java.io.IOException;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.RequestDispatcher;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletRequest;
/*     */ import javax.servlet.ServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FlushCache
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public static final String FLUSH_CACHE = "flushCache";
/*     */   public static final String RES_AJXMSG_JS = "/res/AjxMsg.js";
/*     */   public static final String JS_SKIN_JS = "/js/skin.js";
/*     */   
/*     */   public boolean domainAuthSufficient(Map<String, Object> context)
/*     */   {
/*  65 */     return true;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  70 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*  72 */     Server localServer = Provisioning.getInstance().getLocalServer();
/*  73 */     checkRight(zsc, context, localServer, Rights.Admin.R_flushCache);
/*     */     
/*  75 */     Element eCache = request.getElement("cache");
/*  76 */     String typeStr = eCache.getAttribute("type");
/*  77 */     boolean allServers = eCache.getAttributeBool("allServers", false);
/*     */     
/*  79 */     String[] types = typeStr.split(",");
/*     */     
/*  81 */     for (String type : types) {
/*  82 */       CacheEntryType cacheType = null;
/*     */       try
/*     */       {
/*  85 */         cacheType = CacheEntryType.fromString(type);
/*  86 */         doFlush(context, cacheType, eCache);
/*     */       }
/*     */       catch (ServiceException e) {
/*  89 */         if (cacheType == null)
/*     */         {
/*  91 */           CacheExtension ce = CacheExtension.getHandler(type);
/*  92 */           if (ce != null) {
/*  93 */             ce.flushCache();
/*     */           } else {
/*  95 */             throw e;
/*     */           }
/*     */         } else {
/*  98 */           throw e;
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 103 */     if (allServers) {
/* 104 */       flushCacheOnAllServers(zsc, request);
/*     */     }
/*     */     
/* 107 */     Element response = zsc.createElement(AdminConstants.FLUSH_CACHE_RESPONSE);
/* 108 */     return response;
/*     */   }
/*     */   
/*     */   private void doFlush(Map<String, Object> context, CacheEntryType cacheType, Element eCache) throws ServiceException
/*     */   {
/* 113 */     String mailURL = Provisioning.getInstance().getLocalServer().getMailURL();
/* 114 */     switch (cacheType) {
/*     */     case acl: 
/* 116 */       PermissionCache.invalidateCache();
/* 117 */       break;
/*     */     case all: 
/* 119 */       flushLdapCache(cacheType, eCache);
/* 120 */       Provisioning.getInstance().refreshValidators();
/* 121 */       break;
/*     */     case galgroup: 
/* 123 */       GalGroup.flushCache(getCacheEntries(eCache));
/* 124 */       break;
/*     */     case uistrings: 
/* 126 */       if (WebClientServiceUtil.isServerInSplitMode()) {
/* 127 */         WebClientServiceUtil.flushUistringsCache();
/*     */       } else {
/* 129 */         sendFlushRequest(context, mailURL, "/res/AjxMsg.js");
/* 130 */         sendFlushRequest(context, "/zimbraAdmin", "/res/AjxMsg.js");
/*     */       }
/* 132 */       break;
/*     */     case skin: 
/* 134 */       SkinUtil.flushCache();
/* 135 */       if (WebClientServiceUtil.isServerInSplitMode()) return;
/* 136 */       sendFlushRequest(context, mailURL, "/js/skin.js"); break;
/*     */     
/*     */ 
/*     */     case locale: 
/* 140 */       WebClientL10nUtil.flushCache();
/* 141 */       break;
/*     */     case license: 
/* 143 */       flushLdapCache(CacheEntryType.config, eCache);
/* 144 */       Provisioning.getInstance().refreshValidators();
/* 145 */       break;
/*     */     case zimlet: 
/* 147 */       ZimletUtil.flushDiskCache(context);
/* 148 */       if (!WebClientServiceUtil.isServerInSplitMode()) {
/* 149 */         flushAllZimlets(context);
/*     */       }
/*     */       break;
/*     */     }
/* 153 */     flushLdapCache(cacheType, eCache);
/*     */   }
/*     */   
/*     */   public static void flushAllZimlets(Map<String, Object> context)
/*     */   {
/* 158 */     sendFlushRequest(context, "/service", "/zimlet/res/all.js");
/*     */   }
/*     */   
/*     */   private Provisioning.CacheEntry[] getCacheEntries(Element eCache) throws ServiceException {
/* 162 */     List<Element> eEntries = eCache.listElements("entry");
/* 163 */     Provisioning.CacheEntry[] entries = null;
/* 164 */     int i; if (eEntries.size() > 0) {
/* 165 */       entries = new Provisioning.CacheEntry[eEntries.size()];
/* 166 */       i = 0;
/* 167 */       for (Element eEntry : eEntries) {
/* 168 */         entries[(i++)] = new Provisioning.CacheEntry(Key.CacheEntryBy.valueOf(eEntry.getAttribute("by")), eEntry.getText());
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 173 */     return entries;
/*     */   }
/*     */   
/*     */   private void flushLdapCache(CacheEntryType cacheType, Element eCache) throws ServiceException {
/* 177 */     Provisioning.CacheEntry[] entries = getCacheEntries(eCache);
/* 178 */     Provisioning.getInstance().flushCache(cacheType, entries);
/*     */   }
/*     */   
/*     */   public static void sendFlushRequest(Map<String, Object> context, String appContext, String resourceUri) {
/* 182 */     ServletContext containerContext = (ServletContext)context.get("servlet.context");
/* 183 */     if (containerContext == null) {
/* 184 */       if (ZimbraLog.misc.isDebugEnabled()) {
/* 185 */         ZimbraLog.misc.debug("flushCache: no container context");
/*     */       }
/* 187 */       return;
/*     */     }
/* 189 */     ServletContext webappContext = containerContext.getContext(appContext);
/* 190 */     RequestDispatcher dispatcher = webappContext.getRequestDispatcher(resourceUri);
/* 191 */     if (dispatcher == null) {
/* 192 */       if (ZimbraLog.misc.isDebugEnabled()) {
/* 193 */         ZimbraLog.misc.debug("flushCache: no dispatcher for " + resourceUri);
/*     */       }
/* 195 */       return;
/*     */     }
/*     */     try
/*     */     {
/* 199 */       if (ZimbraLog.misc.isDebugEnabled()) {
/* 200 */         ZimbraLog.misc.debug("flushCache: sending flush request");
/*     */       }
/* 202 */       ServletRequest request = (ServletRequest)context.get("servlet.request");
/* 203 */       request.setAttribute("flushCache", Boolean.TRUE);
/* 204 */       ServletResponse response = (ServletResponse)context.get("servlet.response");
/* 205 */       dispatcher.include(request, response);
/*     */     }
/*     */     catch (Throwable t)
/*     */     {
/* 209 */       if (ZimbraLog.misc.isDebugEnabled()) {
/* 210 */         ZimbraLog.misc.debug("flushCache: " + t.getMessage());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void flushChacheOnServer(Server server, ZimbraSoapContext zsc, String cacheType) throws ServiceException, IOException {
/* 216 */     String adminUrl = URLUtil.getAdminURL(server, "/service/admin/soap/");
/* 217 */     SoapHttpTransport mTransport = new SoapHttpTransport(adminUrl);
/* 218 */     mTransport.setAuthToken(zsc.getRawAuthToken());
/* 219 */     Element.XMLElement req = new Element.XMLElement(AdminConstants.FLUSH_CACHE_REQUEST);
/* 220 */     req.addElement("cache").addAttribute("type", cacheType);
/* 221 */     mTransport.invoke(req);
/*     */   }
/*     */   
/*     */   private void flushCacheOnAllServers(ZimbraSoapContext zsc, Element origReq) throws ServiceException
/*     */   {
/* 226 */     Provisioning prov = Provisioning.getInstance();
/* 227 */     String localServerId = prov.getLocalServer().getId();
/*     */     
/* 229 */     for (Server server : prov.getAllMailClientServers())
/*     */     {
/* 231 */       if (!localServerId.equals(server.getId()))
/*     */       {
/*     */ 
/*     */ 
/* 235 */         ZimbraLog.misc.debug("Flushing cache on server: " + server.getName());
/*     */         
/* 237 */         Element req = origReq.clone();
/* 238 */         Element eCache = req.getElement("cache");
/* 239 */         eCache.addAttribute("allServers", false);
/*     */         
/* 241 */         String adminUrl = URLUtil.getAdminURL(server, "/service/admin/soap/");
/* 242 */         SoapHttpTransport mTransport = new SoapHttpTransport(adminUrl);
/* 243 */         mTransport.setAuthToken(zsc.getRawAuthToken());
/*     */         try
/*     */         {
/* 246 */           mTransport.invoke(req);
/*     */         }
/*     */         catch (ServiceException e) {
/* 249 */           ZimbraLog.misc.warn("Encountered exception while FlushCache on server: " + server.getName() + ", skip and continue with the next server", e);
/*     */         }
/*     */         catch (IOException e)
/*     */         {
/* 253 */           ZimbraLog.misc.warn("Encountered exception while FlushCache on server: " + server.getName() + ", skip and continue with the next server", e);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 262 */     relatedRights.add(Rights.Admin.R_flushCache);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/FlushCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */